package com.example.demo.cmd;

import cn.hutool.core.codec.Base64;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.*;

public class Test1 {

    @Test
    public void test1() throws IOException {
        Map<String, String> stringStringMap = generatePkcs1KeyPairMap();
        System.out.println(stringStringMap);


    }

    @Test
    public void test4() {

        // 用户密码
        String password = "123";
        // 创建密码加密的对象
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        // 密码加密
        String newPassword = passwordEncoder.encode(password);
        System.out.println("加密后的密码为：" + newPassword);

        // 校验这两个密码是否是同一个密码
        // matches方法第一个参数是原密码，第二个参数是加密后的密码
        boolean matches = passwordEncoder.matches(password, newPassword);
        System.out.println("两个密码一致:" + matches);


    }

    @Test
    public void test5() {
        String encode1 = Base64.encode("https://link-wechat-1251309172.cos.ap-nanjing.myqcloud.com/2022/10/24/3c84dc1c-b1d6-4daa-910f-79de72045c35.doc");
        String encode = URLEncoder.encode(encode1);
        System.out.println(encode);
    }


    @Test
    public void test6() {
        String str = "aeasdasd123dasdasduyhjgtyfghyfgh";
        int i = str.hashCode();
        System.out.println(i);
    }




    @Test
    public void test3() throws IOException {
        List<String> list = new ArrayList<>();
        list.add("1");
        List<String> strings = list.subList(0, 5);
    }

    @Test
    public void test2() throws IOException, InterruptedException {
        String openssl_version = executeCmd("openssl version -a");
        System.out.println(openssl_version);
    }

    /**
     * 判断是否是windows系统
     */
    public void isWindows() {
        String osName = System.getProperties().getProperty("os.name");
        System.out.println(osName.toUpperCase().indexOf("WINDOWS") != -1);
    }


    public static synchronized Map<String, String> generatePkcs1KeyPairMap() throws IOException {
        Map<String, String> keyPairMap = new HashMap<>(2);
        String filePath = System.getProperty("java.io.tmpdir");
        if (!filePath.endsWith(File.separator)) {
            filePath += File.separator;
        }
        System.out.println("filePath的路径" + filePath);
        String privateCmd = "openssl genrsa -out " + filePath + "privateKey.pem 2048";
        String publicCmd = "openssl  rsa -in  " + filePath + "privateKey.pem -pubout -out " + filePath + "publicKey.pem";

        Map<String, String> map = new HashMap<>();
        map.put("privateKey", privateCmd);
        map.put("publicKey", publicCmd);

        String finalFilePath = filePath;
        map.forEach((k, v) -> {
            Process p = null;
            try {
                System.out.println("执行命令:" + v);
                p = Runtime.getRuntime().exec(v);
                ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream();
                InputStream errorInStream = new BufferedInputStream(p.getErrorStream());
                InputStream processInStream = new BufferedInputStream(p.getInputStream());
                int num = 0;
                byte[] bs = new byte[1024];
                while ((num = errorInStream.read(bs)) != -1) {
                    resultOutStream.write(bs, 0, num);
                }
                while ((num = processInStream.read(bs)) != -1) {
                    resultOutStream.write(bs, 0, num);
                }
                String result = new String(resultOutStream.toByteArray(), "gbk");
                System.out.println("执行结果:" + result);
                errorInStream.close();
                processInStream.close();
                resultOutStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (p != null) {
                    p.destroy();
                }
                ByteArrayOutputStream resultOutStream = new ByteArrayOutputStream();
                BufferedInputStream fs = null;
                try {
                    fs = new BufferedInputStream(new FileInputStream(new File(finalFilePath + k + ".pem")));
                    int num = 0;
                    byte[] bs = new byte[1024];
                    while ((num = fs.read(bs)) != -1) {
                        resultOutStream.write(bs, 0, num);
                    }
                    String result = new String(resultOutStream.toByteArray(), "gbk");
                    keyPairMap.put(k, result);
                } catch (FileNotFoundException e) {
                    e.getMessage();
                } catch (UnsupportedEncodingException e) {
                    e.getMessage();
                } catch (IOException e) {
                    e.getMessage();
                } finally {
                    if (resultOutStream != null) {
                        try {
                            resultOutStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fs != null) {
                        try {
                            fs.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
        return keyPairMap;
    }


    public static String executeCmd(String command) throws IOException, InterruptedException {

        System.out.println("Execute command : " + command);

        Runtime runtime = Runtime.getRuntime();

        Process process = runtime.exec("cmd.exe /c " + command);

        int i = process.waitFor();
        if (i != 0) {
            System.out.println("cmd任务执行失败");
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8"));

        String line = null;

        StringBuilder build = new StringBuilder();

        while ((line = br.readLine()) != null) {
            System.out.println(line);
            build.append(line);
        }
        return build.toString();

    }





}
